
package info.niwota.ispy;

import java.io.IOException;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings.SettingNotFoundException;

/**
 * 
 * @author qiangli
 *
 */
public class Utils {

    private static final String CIQ_LOG_KEY = "iqagent_logging";
    
    public static void logcatClear() {
    	try {
			Runtime.getRuntime().exec("logcat -c");
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    public static boolean hasCIQLogging(Context ctx) {
		try {
	    	final ContentResolver cr = ctx.getContentResolver();
			android.provider.Settings.System.getInt(cr, CIQ_LOG_KEY);
			return true;
		} catch (SettingNotFoundException e) {
			//
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
    }
    
    public static boolean isCIQLoggingEnabled(Context ctx) {
		try {
	    	final ContentResolver cr = ctx.getContentResolver();
			int v = android.provider.Settings.System.getInt(cr, CIQ_LOG_KEY);
			return v != 0;
		} catch(SettingNotFoundException e) {
			//
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
    }
    
	public static boolean toggleCIQLogging(Context ctx, boolean enable) {
		try {
			final ContentResolver cr = ctx.getContentResolver();
			android.provider.Settings.System.getInt(cr, CIQ_LOG_KEY); //ensure ciq logging entry exist
			android.provider.Settings.System.putInt(cr, CIQ_LOG_KEY, (enable ? 1 : 0));
			return true;
		} catch(SettingNotFoundException e) {
			//
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
    
}
